#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k)
    {
        std::unordered_map<int, int> prevSum;
        prevSum[0] = -1;

        int n = nums.size();
        int remainder = 0;
        for (int i = 0; i < n; ++i)
        {
            remainder = (remainder + nums[i]) % k;
            if (prevSum.count(remainder))
            {
                if (i - prevSum[remainder] >= 2)
                {
                    return true;
                }
            }
            else
            {
                prevSum[remainder] = i;
            }
        }

        return false;
    }
};